//
//  CheckGoodsInforViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/21.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "CheckGoodsInforViewModel.h"
#import "ShopCarModel.h"
#import "NoDataFootTableViewHeaderFooterView.h"
#import "CheckGoodsInforTableViewCell.h"
#import "CheckGoodsInforTableViewHeaderFooterView.h"
#import "CheckBottomView.h"


@interface CheckGoodsInforViewModel ()<UITableViewDelegate,UITableViewDataSource>{
    //标记
    NSInteger jiSuMark;
    NSInteger puTongMark;
    NSInteger queHuoMark;
}
@property(nonatomic,strong) NSMutableArray<ShopCarModel_Data *> * jiSuArr;//极速
@property(nonatomic,strong) NSMutableArray<ShopCarModel_Data *> * puTongArr;//普通
@property(nonatomic,strong) NSMutableArray<ShopCarModel_Data *> * queHuoArr;//缺货

@property(nonatomic,strong) NSMutableArray * sectionDataArr;//section
@end

@implementation CheckGoodsInforViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView theGoodsArr:(nonnull NSMutableArray *)goodsArr{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        self.jiSuArr = [NSMutableArray arrayWithCapacity:0];
        self.puTongArr = [NSMutableArray arrayWithCapacity:0];
        self.queHuoArr = [NSMutableArray arrayWithCapacity:0];
        self.sectionDataArr = [NSMutableArray arrayWithCapacity:0];
        jiSuMark = 0;
        puTongMark = 0;
        queHuoMark = 0;
        CGFloat money = 0.00;
        //选择好
        for (ShopCarModel_Data * l_Data in goodsArr) {
            money = money + l_Data.num*l_Data.price;
            if (l_Data.productQuantity == 0) {
                [self.queHuoArr addObject:l_Data];
            }else{
                if (l_Data.tradingMode == 1) {
                    [self.puTongArr addObject:l_Data];
                }else{
                    [self.jiSuArr addObject:l_Data];
                }
            }
        }
        //排好
        for (ShopCarModel_Data * s_data in self.jiSuArr) {
            if (self->jiSuMark == s_data.shopId) {
                s_data.isSimilar = NO;
            }else{
                s_data.isSimilar = YES;
                self->jiSuMark = s_data.shopId;
            }
        }
        for (ShopCarModel_Data * p_data in self.puTongArr) {
            if (self->puTongMark == p_data.shopId) {
                p_data.isSimilar = NO;
            }else{
                p_data.isSimilar = YES;
                self->puTongMark = p_data.shopId;
            }
        }
        for (ShopCarModel_Data * q_data in self.queHuoArr) {
            if (self->queHuoMark == q_data.shopId) {
                q_data.isSimilar = NO;
            }else{
                q_data.isSimilar = YES;
                self->queHuoMark = q_data.shopId;
            }
        }
        //
        if (self.jiSuArr.count>0) {
            ShopCarModel_Header * jiSuHeader = [ShopCarModel_Header new];
            jiSuHeader.sectionBool = NO;
            jiSuHeader.titleImageStr = @"icon_jisujiaoyi";
            jiSuHeader.titleStr = @" 极速交易商品";
            jiSuHeader.dataSourceArr = self.jiSuArr;
            [self.sectionDataArr addObject:jiSuHeader];
        }
        //
        if (self.puTongArr.count>0) {
            ShopCarModel_Header * puTongHeader = [ShopCarModel_Header new];
            puTongHeader.sectionBool = NO;
            puTongHeader.titleImageStr = @"icon_changqiqiugou";
            puTongHeader.titleStr = @" 普通商品";
            puTongHeader.dataSourceArr = self.puTongArr;
            [self.sectionDataArr addObject:puTongHeader];
        }
        //
        if (self.queHuoArr.count>0) {
            ShopCarModel_Header * queHuoHeader = [ShopCarModel_Header new];
            queHuoHeader.sectionBool = NO;
            queHuoHeader.titleImageStr = @"icon_quehuo";
            queHuoHeader.titleStr = @" 缺货商品";
            queHuoHeader.dataSourceArr = self.queHuoArr;
            [self.sectionDataArr addObject:queHuoHeader];
        }
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(62);
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.rowHeight = UITableViewAutomaticDimension;
        [tableView registerClass:[NoDataFootTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"NoDataFootTableViewHeaderFooterView"];
        [tableView registerClass:[CheckGoodsInforTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"CheckGoodsInforTableViewHeaderFooterView"];
        [superView addSubview:tableView];
        //
        CheckBottomView * bottomBut = [[CheckBottomView alloc] initWithFrame:CGRectMake(0, kY_With_View(tableView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(62)) inSuperView:superView];
        bottomBut.moneyFolot = money;
        bottomBut.Block_BottomAction = ^(GLGWButton * _Nonnull b_But) {
            if (b_But.tag == 300) {//放回
                if (self.delegate && [self.delegate respondsToSelector:@selector(checkGoodsBank:)]) {
                    [self.delegate checkGoodsBank:NO];
                }
            }else{//提交
                
            }
        };
    }
    return self;
}


#pragma mark --UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.sectionDataArr.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    ShopCarModel_Header * h_model = self.sectionDataArr[section];
    return h_model.dataSourceArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShopCarModel_Header * h_model = self.sectionDataArr[indexPath.section];
    ShopCarModel_Data * dataModel = h_model.dataSourceArr[indexPath.row];
    CheckGoodsInforTableViewCell * cell = [CheckGoodsInforTableViewCell cellWithTableView:tableView];
    cell.productModel = dataModel;
    [cell setSepartionLineWith:lineModel_All];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ShopCarModel_Header * hraderModel = self.sectionDataArr[section];
    CheckGoodsInforTableViewHeaderFooterView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CheckGoodsInforTableViewHeaderFooterView"];
    headerView.s_headerModel = hraderModel;
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kHeightProportion_In_iPhoneTriploid(50);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    NoDataFootTableViewHeaderFooterView * footView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
@end
